﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfControlLibrary
{
    internal partial class LogInfo : ObservableObject
    {
        public LogInfo(string log)
        {
            //string timeStamp = DateTime.Now.ToString("MM-dd HH:mm:ss");
            string timeStamp = DateTime.Now.ToString("HH:mm:ss");
            Time = timeStamp;

            IsOk = !log.Contains("!");
            Content = log;
        }

        [ObservableProperty] private string _time = string.Empty;

        [ObservableProperty] private string _content = string.Empty;

        [ObservableProperty] private bool _isOk = true;
    }

    /// <summary>
    /// LogView.xaml 的交互逻辑
    /// </summary>

    public partial class LogView : UserControl
    {
        private readonly ObservableCollection<LogInfo> _logs = new ObservableCollection<LogInfo>();

        /// <summary>
        /// ctor
        /// </summary>
        public LogView()
        {
            InitializeComponent();
            GdLogs.ItemsSource = this._logs;
        }

        private readonly Lock _locker = new Lock();

        /// <summary>
        /// update log
        /// </summary>
        /// <param name="str"> </param>
        public void LogUpdate(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;
            if (_logs?.Count > 10000)
            {
                _logs?.Clear();
            }
            lock (_locker)
            {
                _logs?.Add(new LogInfo(str));
                GdLogs?.ScrollIntoView(_logs![_logs.Count - 1]);
            }
        }

        /// <summary>
        /// 背景颜色
        /// </summary>
        public Brush BackColor
        {
            get => (Brush)GetValue(BackColorProperty);
            set => SetValue(BackColorProperty, value);
        }

        /// <summary>
        /// 背景颜色
        /// </summary>
        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColorProperty", typeof(Brush), typeof(LogView), new PropertyMetadata(Brushes.Transparent));

        /// <summary>
        /// 字体颜色
        /// </summary>
        public Brush FontColor
        {
            get => (Brush)GetValue(FontColorProperty);
            set => SetValue(FontColorProperty, value);
        }

        /// <summary>
        /// 字体颜色依赖属性
        /// </summary>
        public static readonly DependencyProperty FontColorProperty =
            DependencyProperty.Register(nameof(FontColor), typeof(Brush), typeof(LogView), new PropertyMetadata(Brushes.White));

        /// <summary>
        /// 错误日志背景颜色
        /// </summary>
        public Brush NgBackColor
        {
            get => (Brush)GetValue(NgBackColorProperty);
            set => SetValue(NgBackColorProperty, value);
        }

        /// <summary>
        /// 错误日志背景颜色依赖属性
        /// </summary>
        public static readonly DependencyProperty NgBackColorProperty =
            DependencyProperty.Register("NgBackColorProperty", typeof(Brush), typeof(LogView), new PropertyMetadata(Brushes.OrangeRed, new PropertyChangedCallback(ColorPropertyChangedCallback)));

        private static void ColorPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as LogView)?.ColorChanged();
        }

        private void ColorChanged()
        {
        }

        /// <summary>
        /// log
        /// </summary>
        public string Log
        {
            get => (string)GetValue(LogProperty);
            set => SetValue(LogProperty, value);
        }

        /// <summary>
        /// log 依赖属性
        /// </summary>
        public static readonly DependencyProperty LogProperty =
            DependencyProperty.Register(nameof(Log), typeof(string), typeof(LogView), new PropertyMetadata(string.Empty, new PropertyChangedCallback(LogPropertyChangedCallback)));

        private static void LogPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as LogView)?.LogChanged();
        }

        private void LogChanged()
        {
            LogUpdate(Log);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            _logs.Clear();
        }
    }
}